import 'dart:async';

import 'request_state.dart';

typedef StreamWrapper = Stream<Resource<T>> Function<T>(Stream<Resource<T>> Function() function, {bool retry});
typedef FutureWrapper = Future<Resource<T>> Function<T>(Future<Resource<T>> Function() function, {bool retry});


class DefaultWrapper{

  //流的方式
  static Stream<Resource<T>> customStreamWrapper<T>(
      Stream<Resource<T>> Function() function,
      {bool retry = false}) async* {
    yield Resource.loading();
    try {
      var result = function.call();
      yield* result;
    } catch (e) {
      yield Resource.failed(e);
    }
  }

  //异步的方式
  static Future<Resource<T>> customFutureWrapper<T>(
      FutureOr<Resource<T>> Function() function,
      {bool retry = false}) async {
    try {
      var result = function.call();
      if (result is Future) {
        var _result = await result;
        return _result;
      }
      return result;
    } catch (e) {
      return Resource.failed(e);
    }
  }
}